Telegram Group & Telegram Channel
🖥 Прочитать Nullable<long> из строки

Итак, дан код:

long? catCode = null;
// Если строка val не пустая,
if (!(String.IsNullOrWhiteSpace(val)))
{
// распознаем в первых цифрах до пробела целое 64-битное число.
// Если его там нет, записываем в catCode NULL.
// Если его удалось распознать, записываем его в catCode.
if (!(Int64.TryParse(val.Split(" ")[0], out long cc)))
{
catCode = null;
}
else
{
catCode = cc;
}
}

Как переписать этот код чуть понятнее?

Как вариант, можно написать что-то в духе:

public static string LeftDigits(this string str)
{
if (str == null) return null;
if (!str.contains(" ") return null;
string left = str.Split(" ")[0];
if (! left.All(Char.IsDigit){
return null;
}
return left;
}

//...

string digits = LeftDigits(val);
long? catCode = digits != null ? Int64.Parse(digits) : null;

А есть ли ещё варианты?
Можете накидать свои в комментах

@csharp_1001_notes
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/csharp_1001_notes/433
Create:
Last Update:

🖥 Прочитать Nullable<long> из строки

Итак, дан код:


long? catCode = null;
// Если строка val не пустая,
if (!(String.IsNullOrWhiteSpace(val)))
{
// распознаем в первых цифрах до пробела целое 64-битное число.
// Если его там нет, записываем в catCode NULL.
// Если его удалось распознать, записываем его в catCode.
if (!(Int64.TryParse(val.Split(" ")[0], out long cc)))
{
catCode = null;
}
else
{
catCode = cc;
}
}

Как переписать этот код чуть понятнее?

Как вариант, можно написать что-то в духе:

public static string LeftDigits(this string str)
{
if (str == null) return null;
if (!str.contains(" ") return null;
string left = str.Split(" ")[0];
if (! left.All(Char.IsDigit){
return null;
}
return left;
}

//...

string digits = LeftDigits(val);
long? catCode = digits != null ? Int64.Parse(digits) : null;

А есть ли ещё варианты?
Можете накидать свои в комментах

@csharp_1001_notes

BY C# 1001 notes




Share with your friend now:
tg-me.com/csharp_1001_notes/433

View MORE
Open in Telegram


C 1001 notes Telegram | DID YOU KNOW?

Date: |

Mr. Durov launched Telegram in late 2013 with his brother, Nikolai, just months before he was pushed out of VK, the Russian social-media platform he founded. Mr. Durov pitched his new app—funded with the proceeds from the VK sale—less as a business than as a way for people to send messages while avoiding government surveillance and censorship.

That growth environment will include rising inflation and interest rates. Those upward shifts naturally accompany healthy growth periods as the demand for resources, products and services rise. Importantly, the Federal Reserve has laid out the rationale for not interfering with that natural growth transition.It's not exactly a fad, but there is a widespread willingness to pay up for a growth story. Classic fundamental analysis takes a back seat. Even negative earnings are ignored. In fact, positive earnings seem to be a limiting measure, producing the question, "Is that all you've got?" The preference is a vision of untold riches when the exciting story plays out as expected.

C 1001 notes from ca


Telegram C# 1001 notes
FROM USA